/**
 * The medical employee can be either a nurse, doctor,
 * or system admin. Only the doctor, and system admin
 * have separate subclasses, they are differentiated
 * by their employeeType property
 */
package docoffice.doss;

import java.sql.ResultSet;
import java.util.logging.Logger;

import docoffice.database.DatabaseManager;



/**
 * @author Ray Nosari
 * @version $Revision: 1.0 $
 * 
 */
public class MedicalEmployee extends User{
	
	/**
	 * Fields name, yearsExperience, and employeeType
	 */
	private String name, employeeType;
	
	/**
	 * Field yearsExperience, id.
	 */
	private int yearsExperience, id;
	
	/**
	 * Field LOG.
	 */
	private static final Logger LOG = Logger.getLogger("MedicalEmployee");
	
	/**
	 * Field NURSE.
	 */
	private static final int NURSE = 1;
	
	/**
	 * Field DOCTOR.
	 */
	private static final int DOCTOR = 2;
	
	/**
	 * Field SYSADMIN.
	 */
	private static final int SYSADMIN = 3;
	
	/**
	 * Constructor for MedicalEmployee.
	 * @param name String
	 * @param employeeType String
	 */
	public MedicalEmployee(String name, String employeeType){
		this.name = name;
		this.employeeType = employeeType;
		this.setUserType(employeeType);
	}
	
	/**
	 * Constructor for MedicalEmployee.
	 * @param name String
	 * @param employeeType String
	 * @param yearsExperience int
	 */
	public MedicalEmployee(String name, String employeeType, int yearsExperience){
		this.name = name;
		this.employeeType = employeeType;
		this.setUserType(employeeType);
		this.yearsExperience = yearsExperience;
	}
	
	/**
	 * Method getName.
	
	 * @return String */
	public String getName(){
		return name;
	}
	
	/**
	 * Method setName.
	 * @param name String
	 */
	public void setName(String name){
		this.name = name;
	}
	
	/**
	 * Method getEmployeeType.
	
	 * @return String */
	public String getEmployeeType(){
		return employeeType;
	}
	
	/**
	 * Method setEmployeeType.
	 * @param employeeType String
	 */
	public void setEmployeeType(String employeeType){
		this.employeeType = employeeType;
	}
	
	/**
	 * Method getYearsExperience.
	
	 * @return int */
	public int getYearsExperience(){
		return yearsExperience;
	}
	
	/**
	 * Method setYearsExperience.
	 * @param yearsExperience int
	 */
	public void setYearsExperience(int yearsExperience){
		this.yearsExperience = yearsExperience;
	}
	
	/**
	 * getter for id
	
	 * @return id */
	public int getId(){
		return this.id;
	}
	
	/**
	 * setter for id
	 * @param id
	 */
	public void setId(int id){
		this.id = id;
	}
	
	/**
	 * Method toString.
	
	 * @return String */
	public String toString(){
		final String ret = "Medical Employee class can either be a " +
				"nurse, doctor, or system admin";
		return ret;
	}
	
	/**
	 * createUser overload
	 * @param username String
	 * @param name String
	 */
	public void createMedicalEmployee(){
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "INSERT INTO medicalemployee " +
				"(Username, EmployeeName) VALUES ('" + this.getUsername() + "', '" + this.name + "')";
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while creating an employee");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		this.id = this.readMedicalEmployeeID();
	}
	
	/**
	 * readUser overload
	 * @param employee
	 * @return employee 
	 */
	public MedicalEmployee readMedicalEmployee(){
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT * From medicalemployee WHERE" +
				" EmployeeID = " + this.id;
		ResultSet rs = null;
		MedicalEmployee employee = new MedicalEmployee("", "");
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			final String username = rs.getString("Username");
			final String name = rs.getString("EmployeeName");
			final int experience = rs.getInt("YearsExperience");
			final int empType = rs.getInt("EmployeeType");
			String employeeType = "";
			switch(empType){
			    case NURSE: employeeType = "Nurse"; 
			    break;
			    case DOCTOR: employeeType = "Doctor"; 
			    break;
			    case SYSADMIN: employeeType = "SysAdmin"; 
			    break;
			    default: employeeType = "";
			}
			employee.setUsername(username);
			employee.setName(name);
			employee.setYearsExperience(experience);
			employee.setEmployeeType(employeeType);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading employee info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		LOG.info("Employee info read successfully");
		return employee;
	}
	
	/**
	 * updateUser overload
	 * @param employee
	 */
	public void updateMedicalEmployee(){
		final String username = this.getUsername();
		final String employeeType = this.getEmployeeType();
		int empType = -1;
		if (employeeType.equals("Nurse")){
			empType = NURSE;
		}
		else if(employeeType.equals("Doctor")){
			empType = DOCTOR;
		}
		else if(employeeType.equals("SysAdmin")){
			empType = SYSADMIN;
		}
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "UPDATE medicalemployee SET Username = '" + username + 
		        "', EmployeeName = '" + this.name + "', YearsExperience = '" + this.yearsExperience + 
		        "', EmployeeType = '" + empType + "' WHERE EmployeeID = " + id;

		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while updating employee info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		LOG.info("Employee updated successfully");
	}
	
	/**
	 * deleteUser overload
	 * @param employee
	 */
	public void deleteMedicalEmployee(){
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "DELETE FROM medicalemployee WHERE EmployeeID = " + this.id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while deleting employee info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Employee deleted successfully");
	}
	
	/**
	 * Method readMedicalEmployeeID.
	 * @param employee MedicalEmployee
	 * @return int
	 */
	public int readMedicalEmployeeID(){
		int id = 0;
		final String username = this.getUsername();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT EmployeeID FROM medicalemployee" +
				" WHERE Username = '" + username + "'";
		ResultSet rs = null;

		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			id = rs.getInt("EmployeeID");
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while getting a employee id");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Successfully read employee id");
		return id;
	}
	
}